<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

use app\common\BaseHelper as Helper;
use app\common\Common;
/*
 * 优惠券统计
 * */
class Promotioncount extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = 'membercoupon';
        $defaultWhere = $this->getDefaultWhere();
        $this->defaultWhere = $defaultWhere;
        $get_coupon_purpose = get_coupon_purpose();
        $this->assign('get_coupon_purpose',$get_coupon_purpose);
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        if($request->isAjax()){
            $model = model($this->model);

            // 每页起始条数
            $start = ($param['page'] - 1) * $param['limit'];
            // 每页显示条数
            $length = $param['limit'];
            // 排序条件
            $order = '';

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            if($this->defaultOrder){
                $order = $this->defaultOrder;
            }

            $fields = 'a.status,a.create_date,a.use_date,m.account_number,c.name as coupon_name, c.coupon_purpose ,c.id as coupon_id';
            $data = $model->alias('a')
                ->where($where)
                ->join('member m','a.member_id = m.id')
                ->join('coupon c','a.coupon_id = c.id')
                ->field($fields)
                ->limit($start,$length)
                ->order($order)
                ->select();

            $couponMdl = model('coupon');
            $time = date('Y-m-d H:i:s',time());

            foreach($data as $k => $v)
            {
                $v['key'] = $k + 1;
                $v['coupon_purpose'] = show_coupon_purpose($v['coupon_purpose']);
                $where1 = [
                    'end_time' => ['lt',$time],
                    'id'    => $v['coupon_id'],
                ];
                if (empty($v['use_date'])){
                    $v['use_date'] = '-';
                }

                $coupon = $couponMdl->where($where1)->find();

                if (!empty($coupon) && $v['status'] == 1){
                    $v['status'] = '未使用';
                }else if($v['status'] == 2){
                    $v['status'] = '已使用';
                }
                if (empty($coupon) && $v['status'] != '已使用'){
                    $v['status'] = '已过期';
                }
                if (!empty($where['c.end_time']) && $where['c.end_time'] == ['gt',$time]){
                    if (empty($coupon)){
                         $v['status'] = '已过期';
                    }
                }
            }

            //$count = count($data);
            $count = $model->alias('a')->where($where)->join('member m','a.member_id = m.id')->join('coupon c','a.coupon_id = c.id')->count();
            $result = [
                'code' => '0',
                'msg' => '请求成功',
                'data' => $data,
                'count' => $count,
            ];

            return json($result);
        }

        return self::show($this->indexView, [
            'pageSize' => ['url' => fullUrl($request->path())],
            'searchFields' => $this->searchFields,
            'param' => $request->param()
        ]);
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if(isset($param['name']) && $param['name']){
            $where['c.name'] = ['like', '%'.$param['name'].'%'];
        }
        if(isset($param['account_number']) && $param['account_number']){
            $where['m.account_number'] = ['like', '%'.$param['account_number'].'%'];
        }
        if(isset($param['coupon_purpose']) && $param['coupon_purpose']){
            $where['c.coupon_purpose'] = $param['coupon_purpose'];
        }
        if(isset($param['time']) && $param['time']){
            $time = date('Y-m-d H:i:s',time());
            if ($param['time']  == 3){
                $where['c.end_time'] = ['gt',$time];
            }else if ($param['time'] == 1){
                $where['a.status'] = $param['time'];
                $where['c.end_time'] = ['lt',$time];
            }else{
                $where['a.status'] = $param['time'];
            }
        }

        return $where;
    }

    private function show($template, $data = []) {

        return $this->view->fetch($template, $data);
    }
}
